package com.caigou.modules.channel.ui

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.caigou.comm.base.bean.AppBackgroundInfo
import com.caigou.comm.base.container.BaseFragment
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.ext.observe
import com.caigou.comm.base.ext.showToast
import com.caigou.modules.channel.R
import com.caigou.modules.channel.adapter.BlackAdapter
import com.caigou.modules.channel.adapter.FollowAdapter
import com.caigou.modules.channel.adapter.FriendAdapter
import com.caigou.modules.channel.bean.*
import com.caigou.modules.channel.databinding.FragmentContactBinding
import com.caigou.modules.channel.viewmodel.ChannelViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2021/10/28
 */
@AndroidEntryPoint
class ContactFragment : BaseFragment() {

    private var _binding: FragmentContactBinding? = null
    private val binding get() = _binding!!

    private val channelViewModel: ChannelViewModel by viewModels()

    private val friendList = mutableListOf<FriendItemBean>()
    private val followList = mutableListOf<FollowItemBean>()
    private val blackList = mutableListOf<BlackItemBean>()


    private var friendPage = 1
    private val friendLimit = 10
    private var friendTotalPage = 0

    private var followPage = 1
    private val followLimit = 10
    private var followTotalPage = 0

    private var blackPage = 1
    private val blackLimit = 10
    private var blackTotalPage = 0

    private var showFriendList = false
    private var showFollowList = false
    private var showBlackList = false

    private val linearLayoutManager: LinearLayoutManager by lazy {
        LinearLayoutManager(requireActivity())
    }

    private val linearLayoutManager2: LinearLayoutManager by lazy {
        LinearLayoutManager(requireActivity())
    }
    private val linearLayoutManager3: LinearLayoutManager by lazy {
        LinearLayoutManager(requireActivity())
    }

    private val friendAdapter: FriendAdapter by lazy {
        FriendAdapter(friendList)
    }

    private val followAdapter: FollowAdapter by lazy {
        FollowAdapter(followList)
    }

    private val blackAdapter: BlackAdapter by lazy {
        BlackAdapter(blackList)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentContactBinding.inflate(inflater, container, false)
        return _binding?.root
    }

    override fun initView() {
        binding.contactSwipeRefreshLayout.run {
            setColorSchemeColors(resources.getColor(R.color.caigou_theme_color))
            setOnRefreshListener {
                isRefreshing = false
                friendAdapter.loadMoreModule.isEnableLoadMore = false
                followAdapter.loadMoreModule.isEnableLoadMore = false
                blackAdapter.loadMoreModule.isEnableLoadMore = false

                friendPage = 1
                followPage = 1
                blackPage = 1


                binding.contactLayoutFriend.contactRecyclerViewList.visibility = View.GONE
                binding.contactLayoutFriend.contactIvArrow.setImageResource(R.drawable.img_channel_right)
                showFriendList = false

                binding.contactLayoutFollow.contactRecyclerViewList.visibility = View.GONE
                binding.contactLayoutFollow.contactIvArrow.setImageResource(R.drawable.img_channel_right)
                showFollowList = false

                binding.contactLayoutBlack.contactRecyclerViewList.visibility = View.GONE
                binding.contactLayoutBlack.contactIvArrow.setImageResource(R.drawable.img_channel_right)
                showBlackList = false

                friendAdapter.setList(mutableListOf())
                followAdapter.setList(mutableListOf())
                blackAdapter.setList(mutableListOf())
            }
        }

        friendAdapter.loadMoreModule.run {
            setOnLoadMoreListener {
                friendAdapter.loadMoreModule.isEnableLoadMore = true
                channelViewModel.getFriendList(friendPage, friendLimit, AppBackgroundInfo.instance.userBean!!.userId)
            }
            isAutoLoadMore = true
            isEnableLoadMoreIfNotFullPage = false
        }

        binding.contactLayoutFriend.contactRecyclerViewList.run {
            layoutManager = linearLayoutManager
            adapter = friendAdapter
            itemAnimator = DefaultItemAnimator()
        }

        binding.contactLayoutFriend.contactIvArrow.setOnClickListener {
            friendPage = 1
            followTotalPage = 0
            if (showFriendList) {
                friendAdapter.setList(mutableListOf())
                showFriendList = false
                binding.contactLayoutFriend.contactRecyclerViewList.visibility = View.GONE
                binding.contactLayoutFriend.contactIvArrow.setImageResource(R.drawable.img_channel_right)
            } else {
                channelViewModel.getFriendList(friendPage, friendLimit, AppBackgroundInfo.instance.userBean!!.userId)
                showFriendList = true
                binding.contactLayoutFriend.contactRecyclerViewList.visibility = View.VISIBLE
                binding.contactLayoutFriend.contactIvArrow.setImageResource(R.drawable.img_channel_down)
            }
        }

        followAdapter.loadMoreModule.run {
            setOnLoadMoreListener {
                followAdapter.loadMoreModule.isEnableLoadMore = true
                channelViewModel.getFollowList(followPage, followLimit, AppBackgroundInfo.instance.userBean!!.userId)
            }
            isAutoLoadMore = true
            isEnableLoadMoreIfNotFullPage = false
        }

        binding.contactLayoutFollow.contactRecyclerViewList.run {
            layoutManager = linearLayoutManager2
            adapter = followAdapter
            itemAnimator = DefaultItemAnimator()
        }

        binding.contactLayoutFollow.contactIvArrow.setOnClickListener {
            followPage = 1
            followTotalPage = 0
            if (showFollowList) {
                followAdapter.setList(mutableListOf())
                showFollowList = false
                binding.contactLayoutFollow.contactRecyclerViewList.visibility = View.GONE
                binding.contactLayoutFollow.contactIvArrow.setImageResource(R.drawable.img_channel_right)
            } else {
                showFollowList = true
                channelViewModel.getFollowList(friendPage, friendLimit, AppBackgroundInfo.instance.userBean!!.userId)
                binding.contactLayoutFollow.contactRecyclerViewList.visibility = View.VISIBLE
                binding.contactLayoutFollow.contactIvArrow.setImageResource(R.drawable.img_channel_down)
            }
        }

        blackAdapter.loadMoreModule.run {
            setOnLoadMoreListener {
                blackAdapter.loadMoreModule.isEnableLoadMore = true
                channelViewModel.getBlackList(blackPage, blackLimit, AppBackgroundInfo.instance.userBean!!.userId)
            }
            isAutoLoadMore = true
            isEnableLoadMoreIfNotFullPage = false
        }

        binding.contactLayoutBlack.contactRecyclerViewList.run {
            layoutManager = linearLayoutManager3
            adapter = blackAdapter
            itemAnimator = DefaultItemAnimator()
        }

        binding.contactLayoutBlack.contactIvArrow.setOnClickListener {
            blackPage = 1
            blackTotalPage = 0
            if (showBlackList) {
                blackAdapter.setList(mutableListOf())
                showBlackList = false
                binding.contactLayoutBlack.contactRecyclerViewList.visibility = View.GONE
                binding.contactLayoutBlack.contactIvArrow.setImageResource(R.drawable.img_channel_right)
            } else {
                showBlackList = true
                channelViewModel.getBlackList(blackPage, blackLimit, AppBackgroundInfo.instance.userBean!!.userId)
                binding.contactLayoutBlack.contactRecyclerViewList.visibility = View.VISIBLE
                binding.contactLayoutBlack.contactIvArrow.setImageResource(R.drawable.img_channel_down)
            }
        }

        binding.contactTvBack.setOnClickListener {
            (requireActivity() as ChannelActivity).onBackPressed()
        }
    }

    override fun lazyLoad() {
    }

    override fun onBack() {
    }

    override fun observeViewModel() {
        super.observeViewModel()
        observe(channelViewModel.friendLiveData, ::handleFriendList)
        observe(channelViewModel.followLiveData, ::handleFollowList)
        observe(channelViewModel.blackLiveData, ::handleBlackList)

    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    private fun handleFriendList(result: Resource<FriendBean>) {
        when (result) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                friendAdapter.loadMoreModule.isEnableLoadMore = true
                if (!TextUtils.isEmpty(result.errorMsg)) {
                    showToast(result.errorMsg)
                } else {
                    showToast("获取好友信息异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                friendAdapter.loadMoreModule.isEnableLoadMore = true
                if (result.data == null) {
                    return
                }
                friendTotalPage = result.data!!.totalPage

                if (friendPage == 1) {
                    friendAdapter.setList(result.data!!.list)
                } else {
                    friendAdapter.addData(result.data!!.list)
                }

                if (result.data!!.pageSize < friendLimit) {
                    friendAdapter.loadMoreModule.loadMoreEnd(false)
                } else {
                    friendAdapter.loadMoreModule.loadMoreComplete()
                }

                if (friendPage >= friendTotalPage) {
                    friendAdapter.loadMoreModule.loadMoreEnd(false)
                } else {
                    friendPage++
                }
            }
        }
    }

    private fun handleFollowList(result: Resource<FollowBean>) {
        when (result) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                followAdapter.loadMoreModule.isEnableLoadMore = true
                if (!TextUtils.isEmpty(result.errorMsg)) {
                    showToast(result.errorMsg)
                } else {
                    showToast("获取关注信息异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                followAdapter.loadMoreModule.isEnableLoadMore = true
                if (result.data == null) {
                    return
                }
                followTotalPage = result.data!!.totalPage

                if (followPage == 1) {
                    followAdapter.setList(result.data!!.list)
                } else {
                    followAdapter.addData(result.data!!.list)
                }

                if (result.data!!.pageSize < followLimit) {
                    followAdapter.loadMoreModule.loadMoreEnd(false)
                } else {
                    followAdapter.loadMoreModule.loadMoreComplete()
                }

                if (followPage >= followTotalPage) {
                    followAdapter.loadMoreModule.loadMoreEnd(false)
                } else {
                    followPage++
                }
            }
        }
    }

    private fun handleBlackList(result: Resource<BlackBean>) {
        when (result) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                blackAdapter.loadMoreModule.isEnableLoadMore = true
                if (!TextUtils.isEmpty(result.errorMsg)) {
                    showToast(result.errorMsg)
                } else {
                    showToast("获取黑名单信息异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                blackAdapter.loadMoreModule.isEnableLoadMore = true
                if (result.data == null) {
                    return
                }
                blackTotalPage = result.data!!.totalPage

                if (blackPage == 1) {
                    blackAdapter.setList(result.data!!.list)
                } else {
                    blackAdapter.addData(result.data!!.list)
                }

                if (result.data!!.pageSize < blackLimit) {
                    blackAdapter.loadMoreModule.loadMoreEnd(false)
                } else {
                    blackAdapter.loadMoreModule.loadMoreComplete()
                }

                if (blackPage >= blackTotalPage) {
                    blackAdapter.loadMoreModule.loadMoreEnd(false)
                } else {
                    blackPage++
                }
            }
        }
    }
}